﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RondoDance.OM;
using System.Text;

public partial class CategoriesDropDown : System.Web.UI.UserControl
{
    private bool _includeRoot = true;

    private int _maxDepth = -1;

    public int MaxDepth
    {
        get { return _maxDepth; }
        set { _maxDepth = value; }
    }

    public bool IncludeRoot
    {
        get { return _includeRoot; }
        set { _includeRoot = value; }
    }

    public int SelectedIndex
    {
        get { return ddlCategories.SelectedIndex; }
        set { ddlCategories.SelectedIndex = value; }
    }

    public ListItem SelectedItem
    {
        get { return ddlCategories.SelectedItem; }
    }

    public string SelectedValue
    {
        get { return ddlCategories.SelectedValue; }
    }

    public ListItemCollection Items
    {
        get { return ddlCategories.Items; }
    }

    public void ClearSelection()
    {
        ddlCategories.ClearSelection();
    }

    public override string ClientID
    {
        get
        {
            return ddlCategories.ClientID;
        }
    }

    public bool Enabled
    {
        get { return ddlCategories.Enabled; }
        set { ddlCategories.Enabled = value; }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        if (_includeRoot)
        {
            ddlCategories.Items.Add(new ListItem("ראשי", "0"));
        }
        
        // PopulateDropDown(ddlCategories, Steimatzky.Data.SteimatzkyDataAccess.Categories_GetCategoriesTree(), 1);
    }

    private void PopulateDropDown(DropDownList ddlCategoryParent, IList<StaticPage> pages, int indent)
    {
        if (_maxDepth > 0 && indent > _maxDepth)
        {
            return;
        }

        foreach (StaticPage item in pages)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string indentString in Enumerable.Repeat<string>("&nbsp;", indent * 4))
            {
                sb.Append(indentString);
            }

            string text = HttpUtility.HtmlDecode(string.Format("{0}{1}", sb.ToString(), item.PageTitle));
            
            ddlCategoryParent.Items.Add(new ListItem(text, item.Id.ToString()));
        }
    }
}
